/*
 * Copyright (C) 2004-2007  Autodesk, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "Pch.h"
#include "WcsTestCustomCommands.h"
#include "WCS/FdoWcsCommandType.h"
#include "WCS/IGetImageFormats.h"
#include "WCS/IGetFeatureClassStyles.h"
#include "WCS/IGetFeatureClassCRSNames.h"
#include "WCS/IGetCoverageDescription.h"
#include "FdoWcsCoverageDescription.h"
#include "FdoWcsDomain.h"
#include "FdoWcsSpatialDomain.h"
#include "FdoOwsBoundingBoxCollection.h"
#include "FdoOwsBoundingBox.h"
#include "FdoWcsGridCRS.h"

CPPUNIT_TEST_SUITE_REGISTRATION( WcsTestCustomCommands );
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WcsTestCustomCommands, "WcsTestCustomCommands");

WcsTestCustomCommands::WcsTestCustomCommands ()
{
}

WcsTestCustomCommands::~WcsTestCustomCommands ()
{
}

//http://sigma.openplans.org/geoserver/ows
void WcsTestCustomCommands::testGetImageFormats ()
{
    try
    {
        FdoPtr<FdoIConnection> connection = WcsTests::GetConnection ();

        FdoStringP sServer = FdoStringP::Format(L"FeatureServer=http://sigma.openplans.org/geoserver/ows");
        connection->SetConnectionString((FdoString*)sServer);
        FdoConnectionState state = connection->Open ();
        
        //test for formats command
        FdoPtr<FdoWcsIGetImageFormats> cmdTiff = static_cast<FdoWcsIGetImageFormats *> (connection->CreateCommand(FdoWcsCommandType_GetImageFormats));
        cmdTiff->SetFeatureClassName(L"nurc Arc_Sample");
		FdoStringsP formats = cmdTiff->Execute ();
        CPPUNIT_ASSERT(formats->GetCount() == 6);
		CPPUNIT_ASSERT(wcscmp(L"image/tiff", formats->GetString(5)) == 0);
#ifdef _DEBUG
        for (int i = 0; i < formats->GetCount(); ++i)
        {
            wprintf (L"image format: %ls\n", formats->GetString(i));
        }
#endif
        connection->Close ();
    }
    catch (FdoException* e)
    {
        fail(e);
    }
}


void WcsTestCustomCommands::testSpacialFeatureClassName ()
{
    try
    {
        FdoPtr<FdoIConnection> connection = WcsTests::GetConnection ();

        FdoStringP sServer = FdoStringP::Format(L"FeatureServer=http://demo.cubewerx.com/demo/cubeserv/cubeserv.cgi?version=1.1.0");
        connection->SetConnectionString((FdoString*)sServer);
        FdoConnectionState state = connection->Open ();
        
        //test for get styles command
        FdoPtr<FdoWmsIGetFeatureClassStyles> cmdGLS = static_cast<FdoWmsIGetFeatureClassStyles *> (connection->CreateCommand(FdoWcsCommandType_GetFeatureClassStyles));
        cmdGLS->SetFeatureClassName(L"BARRIERL_1M Foundation"); // layer name is BARRIERL_1M:Foundation
        FdoStringsP styles = cmdGLS->Execute();
        CPPUNIT_ASSERT(styles->GetCount() == 1);
#ifdef _DEBUG
        for (int i = 0; i < styles->GetCount(); ++i)
        {
            wprintf (L"Layer Style: %ls\n", styles->GetString(i));
        }
#endif
        //test for get CRS command
        FdoPtr<FdoWcsIGetFeatureClassCRSNames> cmdGLCRS = static_cast<FdoWcsIGetFeatureClassCRSNames *> (connection->CreateCommand(FdoWcsCommandType_GetFeatureClassCRSNames));
        cmdGLCRS->SetFeatureClassName(L"BARRIERL_1M Foundation");//layer name is BARRIERL_1M:Foundation
        FdoStringsP crsNames = cmdGLCRS->Execute();
        CPPUNIT_ASSERT(crsNames->GetCount() == 1);
#ifdef _DEBUG
        for (int i = 0; i < crsNames->GetCount(); ++i)
        {
            wprintf (L"CRS Name: %ls\n", crsNames->GetString(i));
        }
#endif        

        connection->Close ();
    }
    catch (FdoException* e)
    {
        fail(e);
    }
}

void WcsTestCustomCommands::testGetFeatureCRSNames ()
{
    try
    {
        FdoPtr<FdoIConnection> connection = WcsTests::GetConnection ();

        FdoStringP sServer = FdoStringP::Format(L"FeatureServer=http://maps.dnr.state.mn.us/cgi-bin/mapserv50?MAP=/usr/local/www/docs_maps/mapserver_demos/wcs/demo.map");
        connection->SetConnectionString((FdoString*)sServer);
        FdoConnectionState state = connection->Open ();
        
        //test for get CRS command
        FdoPtr<FdoWcsIGetFeatureClassCRSNames> cmdGLCRS = static_cast<FdoWcsIGetFeatureClassCRSNames *> (connection->CreateCommand(FdoWcsCommandType_GetFeatureClassCRSNames));
        cmdGLCRS->SetFeatureClassName(L"fpar");
        FdoStringsP crsNames = cmdGLCRS->Execute();
        CPPUNIT_ASSERT(crsNames->GetCount() == 1);
		CPPUNIT_ASSERT(wcscmp(L"urn:ogc:def:crs:EPSG::26915", crsNames->GetString(0)) == 0);
#ifdef _DEBUG
        for (int i = 0; i < crsNames->GetCount(); ++i)
        {
            wprintf (L"CRS Name: %ls\n", crsNames->GetString(i));
        }
#endif        
        connection->Close ();
    }
    catch (FdoException* e)
    {
        fail(e);
    }
}

void WcsTestCustomCommands::testInvalidParamForCRS ()
{
    FdoPtr<FdoIConnection> connection;
    try
    {
        connection = WcsTests::GetConnection ();
        FdoStringP sServer = FdoStringP::Format(L"FeatureServer=http://nsidc.org/cgi-bin/atlas_north");
        connection->SetConnectionString((FdoString*)sServer);
        FdoConnectionState state = connection->Open ();
        
        //test for get CRS command
        FdoPtr<FdoWcsIGetFeatureClassCRSNames> cmdGLCRS = static_cast<FdoWcsIGetFeatureClassCRSNames *> (connection->CreateCommand(FdoWcsCommandType_GetFeatureClassCRSNames));
        FdoStringsP crsNames = cmdGLCRS->Execute();
        
        connection->Close ();

        std::string errorMsg;
        errorMsg.append("Fail to catch none feature class input error!");
        CPPUNIT_FAIL(errorMsg);
    }
    catch (FdoException* e)
    {
        e->Release();
        connection->Close ();
    }
}

void WcsTestCustomCommands::testGetCoverageDescription()
{
	FdoPtr<FdoIConnection> connection;
	try
	{
		connection = WcsTests::GetConnection ();
		FdoStringP sServer = FdoStringP::Format(L"FeatureServer=http://nsidc.org/cgi-bin/atlas_north");
		connection->SetConnectionString((FdoString*)sServer);
		FdoConnectionState state = connection->Open ();

		//test for get coverage description
		FdoPtr<FdoWcsIGetCoverageDescription> cmdGetCD = static_cast<FdoWcsIGetCoverageDescription *> (connection->CreateCommand(FdoWcsCommandType_GetCoverageDescription));
		cmdGetCD->SetFeatureClassName(L"sea_ice_concentration_08");
		FdoWcsCoverageDescriptionP CDs = cmdGetCD->Execute();
		
		// test the identifier
		CPPUNIT_ASSERT(wcscmp(CDs->GetIdentifier(), L"sea_ice_concentration_08") == 0);
		
		// test the keywords
		FdoStringsP keywords = CDs->GetKeywordList();
		CPPUNIT_ASSERT(keywords->GetCount() == 9);

		// test the domain
		FdoWcsDomainP domain = CDs->GetDomain();
		CPPUNIT_ASSERT(domain != NULL);
	    FdoWcsSpatialDomainP spDomain = domain->GetSpatialDomain();
		CPPUNIT_ASSERT(spDomain != NULL);
		FdoOwsBoundingBoxCollectionP bboxes = spDomain->GetBoundingBoxCollection();
		CPPUNIT_ASSERT(bboxes->GetCount() == 3);
		FdoOwsBoundingBoxP bbox = bboxes->GetItem(0);
		CPPUNIT_ASSERT(wcscmp(bbox->GetCRS(), L"urn:ogc:def:crs:OGC::imageCRS") == 0);
		CPPUNIT_ASSERT(bbox->GetDimensions() == 2);
		CPPUNIT_ASSERT(bbox->GetMaxX() == 316.0);
		CPPUNIT_ASSERT(bbox->GetMaxY() == 332.0);
		FdoWcsGridCRSP gridCRS = spDomain->GetGridCRS();
		CPPUNIT_ASSERT(gridCRS != NULL);
		CPPUNIT_ASSERT(wcscmp(gridCRS->GetGridBaseCRS(), L"urn:ogc:def:crs:EPSG::32661"));
		CPPUNIT_ASSERT(wcscmp(gridCRS->GetGridCS(), L"urn:ogc:def:cs:OGC:0.0:Grid2dSquareCS"));
		CPPUNIT_ASSERT(wcscmp(gridCRS->GetGridType(), L"urn:ogc:def:method:WCS:1.1:2dSimpleGrid"));

		// test SupportedCRS
		FdoStringsP crsNames = CDs->GetSupportedCRSNames();
		CPPUNIT_ASSERT(crsNames->GetCount() == 15);

		// test SupportedFormat
		FdoStringsP formats = CDs->GetSupportedFormatNames();
		CPPUNIT_ASSERT(formats->GetCount() == 1);
		FdoStringElementP strelement = formats->GetItem(0);
		CPPUNIT_ASSERT(wcscmp(strelement->GetString(), L"image/tiff") == 0);

		connection->Close ();

		std::string errorMsg;
		errorMsg.append("Fail to catch none feature class input error!");
		CPPUNIT_FAIL(errorMsg);
	}
	catch (FdoException* e)
	{
		e->Release();
		connection->Close ();
	}
}